﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using Microsoft.WindowsCE.Forms;

namespace XFControls
{
    public partial class XFPanelContainer : UserControl
    {
        protected XFPanelBase mainPanel;
        protected XFPanelHeader header;

        private TextBox _textInput;
        private Button _cancel;
        private Button _submit;

        public XFPanelBase MainPanel { get { return mainPanel; } }
        public XFPanelHeader Header { get { return header; } }
        public IUpdateable UpdateNotification { get; set; }

        public XFPanelContainer()
        {
            InitializeComponent();

            mainPanel = new XFPanelBase();
            mainPanel.Width = ClientSize.Width;

            _inputPanel.EnabledChanged += (o, e) =>
            {
                if (_textInput == null)
                {
                    _inputPanel.Enabled = false;
                    return;
                }
                int width = Screen.PrimaryScreen.WorkingArea.Width;
                int padding = width / 20;
                int buttonPadding = padding + (padding / 2);

                int height = _inputPanel.VisibleDesktop.Height - ((padding * 2) + Header.Height + _submit.Height);
                
                _textInput.Height = height;
                _submit.Location = new Point(padding, height + buttonPadding);
                _cancel.Location = new Point(width - (_cancel.Width + padding), height + buttonPadding);

                _textInput.Invalidate();
            };
        }

        public void Reset()
        {
            mainPanel.Reset();
        }

        public void SetMainPanel(XFPanelBase panel)
        {
            if (mainPanel.RightPanel == null)
                mainPanel.Dispose();

            mainPanel = panel;
            mainPanel.Width = ClientSize.Width;
            if (!this.Controls.Contains(panel))
                this.Controls.Add(panel);

            Reset();
        }

        public void ShowScrollbar(bool show)
        {
            mainPanel.ShowScrollbar(show);
        }

        public void ShowAlphaBar(bool show)
        {
            mainPanel.ShowAlphaBar(show);
        }

        public void SetHeader(XFPanelHeader xfPanelHeader)
        {
            header = xfPanelHeader;
            xfPanelHeader.MainPanelContainer = this;

            if (!Parent.Controls.Contains(xfPanelHeader))
            {
                Parent.Controls.Add(xfPanelHeader);
                xfPanelHeader.Dock = DockStyle.Top;
            }
        }

        public void SetHeaderText(string text)
        {
            if (header != null)
            {
                header.CenterText = text;
                header.Invalidate();
            }
        }

        public string GetHeaderText()
        {
            if (header != null)
            {
                return header.CenterText;
            }
            return null;
        }

        public void SetHeaderRightButton(IXFButton button)
        {
            if (header != null)
            {
                button.Parent = header;
                header.RightButton = button;
                header.Invalidate();
            }
        }

        public void SetHeaderLeftButton(IXFButton button)
        {
            if (header != null)
            {
                button.Parent = header;
                header.LeftButton = button;
                header.Invalidate();
            }
        }

        public void ShowTextInput()
        {
            if (Header != null)
                Header.DisableHeader(true);

            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int padding = width / 20;

            _textInput = new TextBox();
            _cancel = new Button();
            _submit = new Button();

            _submit.Text = "Submit";
            _submit.Height = 50;
            _submit.Width = 100;
            
            _cancel.Text = "Cancel";
            _cancel.Height = 50;
            _cancel.Width = 100;
            
            _textInput.Multiline = true;
            _textInput.Location = new Point(padding, padding);
            _textInput.Width = width - (padding * 2);

            _inputPanel.Enabled = true;
            Application.DoEvents();

            _submit.Click += (o, e) =>
            {
                if (mainPanel != null)
                    mainPanel.OnTextEntered(_textInput.Text);
                cleanUpInput();
            };

            _cancel.Click += (o, e) =>
            {
                if (mainPanel != null)
                    mainPanel.OnTextEnteredCancel();
                cleanUpInput();
            };

            Controls.Add(_textInput);
            Controls.Add(_submit);
            Controls.Add(_cancel);

            _textInput.BringToFront();
            _cancel.BringToFront();
            _submit.BringToFront();
            _textInput.Focus();
        }

        private void cleanUpInput()
        {
            if (Header != null)
                Header.DisableHeader(false);

            if (_textInput != null)
            {
                Controls.Remove(_textInput);
                _textInput.Dispose();
            }

            if (_cancel != null)
            {
                Controls.Remove(_cancel);
                _cancel.Dispose();
            }

            if (_submit != null)
            {
                Controls.Remove(_submit);
                _submit.Dispose();
            }

            _textInput = null;
            _cancel = null;
            _submit = null;

            _inputPanel.Enabled = false;
        }
    }
}
